package com.lhoverc.service.post.impl;

import com.lhoverc.mapper.PostMapper;
import com.lhoverc.pojo.VO.CommentVO;
import com.lhoverc.pojo.VO.PostVO;
import com.lhoverc.pojo.entity.CommentEntity;
import com.lhoverc.pojo.entity.LikeEntity;
import com.lhoverc.pojo.entity.PostEntity;
import com.lhoverc.service.post.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: LiCheng
 * @CreateTime: 2025-05-03
 * @Description:
 * @Version: 1.0
 */
@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;

    @Override
    public List<PostVO> getPosts() {
        List<PostVO> postVOList = postMapper.getPost();
        return postVOList;
    }

    @Override
    public List<CommentVO> getComments(int id) {
        List<CommentVO> commentVOList = postMapper.getComments(id);
        return commentVOList;
    }

    @Override
    public void addComment(CommentEntity commentEntity) {
        LocalDateTime now = LocalDateTime.now();
        commentEntity.setReleaseTime(now);
        commentEntity.setCreateTime(now);
        commentEntity.setUpdateTime(now);
        postMapper.addComment(commentEntity);
    }

    @Override
    public void addPost(PostEntity postEntity) {
        LocalDateTime now = LocalDateTime.now();
        postEntity.setReleaseTime(now);
        postEntity.setCreateTime(now);
        postEntity.setUpdateTime(now);
        postMapper.addPost(postEntity);
    }

    @Override
    public void deleteLike(LikeEntity likeEntity) {
        LocalDateTime now = LocalDateTime.now();
        likeEntity.setUpdateTime(now);
        postMapper.deleteLike(likeEntity);
        postMapper.reducePostLikes(likeEntity);
    }

    @Override
    public void addLike(LikeEntity likeEntity) {
        LocalDateTime now = LocalDateTime.now();
        likeEntity.setCreateTime(now);
        likeEntity.setUpdateTime(now);
        Integer id = postMapper.selectLike(likeEntity);
        if (id == null)
            postMapper.addLike(likeEntity);
        else
            postMapper.updateIsDelete(likeEntity);
        postMapper.addPostLikes(likeEntity);
    }

    @Override
    public List<Integer> getLikes(LikeEntity likeEntity) {
        List<Integer> postIdList = postMapper.getLikes(likeEntity);
        return postIdList;
    }
}
